#include <malloc.h>
#include <stdio.h>
#include <math.h>
#define EOF '>'



//Output methods
void UnderLine()
{
    printf("\n_________________\n");
}
void inputMsg()
{
    UnderLine();
    printf("Input here>");
}

//SolutionMethods
void swap(int* a, int* b)
{
    int temp = *b;
    *b = *a;
    *a = temp;
}
void swapchar(char* a, char* b)
{
    char c;
    c = *a;
    *a = *b;
    *b = c;
}
void ScanMatrix(int* mem, int n, int m)
{
    for(int i = 0; i < n; i++)
        for(int j = 0; j < m; j++)
            scanf("%d", (mem + i * m + j));
    printf("\n");
}

void TransportMatrix(int* mem, int* n, int* m)
{
    swap(n, m);
    for(int i = 0; i < *n; i++)
        for(int j = 0; j < i; j++)
            swap((mem + i* (*m) + j), (mem + j*(*n) + i));
}

void PrintMatrix(int* mem, int n, int m)
{
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
            printf("%d ", *(mem + i*m + j));
        printf("\n");
    }
}
void MirrorMatrix(int* mem, int n, int m)
{
    for(int i = 0; i < n * m / 2; i++)
        swap(mem + i, mem + n * m - i - 1);
}

int primeN(int n)
{
    int counter = n, mark = 1,
        number = 2;
    for(int i = 2; counter > 0; i++)
    {
        for(int j = 2; j <= sqrt(i); j++)
            if(i % j == 0)
            {
                mark = 0;
                break;
            }
        if (mark)
        {
            counter--;
            number = i;
        }
        mark = 1;

    }
    return number;
}
void primeN_Memorization(int n, int* mem)
{
    int counter = n, mark = 1;
    for(int i = 2; counter > 0; i++)
    {
        for(int j = 2; j <= sqrt(i); j++)
            if(i % j == 0)
            {
                mark = 0;
                break;
            }
        if (mark)
        {
            *(mem + n - counter) = i;
            counter--;
        }
        mark = 1;
    }
}
int q(int n, int k)
{
    if ((n == 0 || n == 1) && k > 0) return 1;
    else if (k < 0) return 0;

    return q(n - k, k - 1) + q(n, k - 1);
}

int gcd(int a, int b)
{
    if (a < b) swap(&a, &b);

    int modula = 1;
    while(modula)
    {
        modula = a % b;

        a = b;
        b = modula;
    }

    return a;
}
int gcd3(int a, int b, int c)
{
    int gcdTemp = gcd(a, b);
    return gcd(gcdTemp, c);
}
int lcm (int a, int b)
{
    return a*b / gcd(a, b);
}
int lcm3(int a,int b, int c){
    return lcm(lcm(a, b), c);
}
//Problems
//problem A Variant 2
void problem1()
{
    UnderLine();

    printf("2.A\n");
    int n = 0;
    scanf("%d", &n);

    int* memory = (int*) calloc(n*n, sizeof(int));
    ScanMatrix     (memory,  n,  n);
    TransportMatrix(memory, &n, &n);
    PrintMatrix    (memory,  n,  n);
    free(memory); memory = NULL;
    UnderLine();
}

//problem C Variant 2
void problem2()
{
     UnderLine();
     printf("2.C\n");

     int n = 0;
     scanf("%d", &n);

     n = primeN(n);

     printf("%d\n", n);
}

//problem D variant 4
void problem3()
{
    int n = 0, result = 0;

    UnderLine();
    printf("4.D\n");
    scanf("%d", &n);

    result = q(n,n);

    printf("%d", result);
}
//Problem A variant 3
void problem4()
{
    UnderLine();
    printf("3.A\n");

    int a = 0, b = 0, c = 0;
    scanf("%d %d %d", &a, &b, &c);

    int result = lcm3(a, b, c);

    printf("%d \n", result);
}
//Problem B variant 3
void problem5()
{
    UnderLine();
    printf("3.B\n");

    int n = 0;
    scanf("%d", &n);

    int* memory = (int*) calloc(10000, sizeof(int));
    primeN_Memorization(10000, memory);

    int index = 0;
    int* rezults = (int*) calloc(10000, sizeof(int));

    for(int i = 0; i < n; i++)
    {
        scanf("%d", &index);
        *(rezults + i) = *(memory + index - 1);
    }
    for(int i = 0; i< n; i++)
        printf("%d ", *(rezults+i));
    printf("\n");

    free(rezults); rezults = NULL;
    free(memory) ; memory  = NULL;
}
//problem B variant 4
void problem6()
{
    UnderLine();
    printf("4.B\nEOF line is '>'\n");

    char ch = NULL;
    char* mem = (char*) calloc(10000, sizeof(char));
    int i = 0;

    while((ch = getchar()) != EOF)
    {
        if (ch >= '0' && ch <= '9') ch = '#';
        if (ch >= 'A' && ch <= 'Z') ch += 32;
        if (i > 0 && ch == ' ' &&  *(mem + i - 1) == ' ') i--;
        if (ch == '-' || ch == ',' || ch == '!' || ch == '?' || ch == '.' || ch == '/' || ch == '\\') ch = NULL;
        if (ch != NULL) *(mem + i) = ch;
        else i--;
        i++;
    }

    for (int j = 0; j < i; j++)
        printf("%c", *(mem + j));
    free(mem); mem = NULL;
}
//problem C variant 4
void problem7()
{
    UnderLine();
    printf("4.C\n");
    printf("EOF is '>'. Kostil. I am sorry. \n");

    int index = 0,
        lastIndex = 0,
        length = 0;
    char ch = NULL;
    char* s = (char*) calloc(1001, sizeof(char));

    while(ch = getchar() != '>')
    {
        *(s + length) = ch;
        length++;
    }
    *(s + length) = '\0';

    for(int i = 0; i < length; i++)
    {
        if (*(s + i) == ' ')
        {
            for(int j = lastIndex; j < (i - j) / 2; j++)
                swapchar(s + j, s + (i - 1) - j);
            lastIndex = i + 1;
        }
    }

    index = 0;
    while (ch = *(s + index) != '\0')
        printf("%c", ch);
}
//problem B variant 6
void problem8()
{
    UnderLine();
    printf("6.A\n");

    int n = 0;
    scanf("%d", &n);
    int counter = 0;
    int t = 0,
        h = 0,
        te = 0,
        o = 0;

    if(!(n < 5 || n > 36))
        for(int i = 1000; i < 10000; i++)
        {
            t = (i - i % 1000) / 1000;
            h = (i % 1000 - i % 100) / 100;
            te = (i % 100 - i % 10) / 10;
            o = i % 10;
            if (t + h + te + o == n)
            {
                printf("%d ", i);
                counter++;
            }
        }
     printf("\n%d", counter);

}
//problem A variant 6
void problem9()
{
    UnderLine();
    printf("6.A\n");

    int n = 0, m = 0;
    scanf("%d %d", &n, &m);

    int* matrix = (int*) calloc(n * m, sizeof(int));

    ScanMatrix(matrix, n, m);
    MirrorMatrix(matrix, n, m);
    PrintMatrix(matrix, n, m);

}
//problem A variant 7
void problem10()
{
    UnderLine();
    printf("7.A\n");

    int n = 0;
    scanf("%d", &n);

    for (int i = 1; i <= n; i = i << 1)
        if (n & i) printf("%d ", n & i);
}
int main()
{
    int n = 0;
    while(1)
    {
        UnderLine();
        printf("\nChoose problem (1..10):\n");
        scanf ("%d", &n);
        switch(n)
        {
            case 1: problem1(); break;
            case 2: problem2(); break;
            case 3: problem3(); break;
            case 4: problem4(); break;
            case 5: problem5(); break;
            case 6: problem6(); break;
            case 7: problem7(); break;
            case 8: problem8(); break;
            case 9: problem9(); break;
            case 10: problem10(); break;
            default:return 0;
        }

    }
    return 0;
}
